package lmh.example.exam1_6;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.res.ResourcesCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.media.MediaPlayer;
import android.os.Environment;
import android.view.View;
import android.view.WindowManager;

import android.widget.ImageButton;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    public static final int EXTERNAL_STORAGE_REQ_CODE = 10;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        VideoView video = (VideoView) findViewById(R.id.video);            //获取VideoView组件
        //设置全屏显示
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        int permission = ActivityCompat.checkSelfPermission(this,
                Manifest.permission.READ_EXTERNAL_STORAGE);
        if (permission != PackageManager.PERMISSION_GRANTED) {
            // 请求权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, EXTERNAL_STORAGE_REQ_CODE);
        }

        //指定模拟器SD卡上要播放的视频文件
        final File file = new File(Environment.getExternalStorageDirectory() + "/big_buck.mp4");
        if (file.exists()) {                                    //判断要播放的视频文件是否存在
            video.setVideoPath(file.getAbsolutePath());         //指定要播放的视频
        } else {
            //弹出消息提示框提示文件不存在
            Toast.makeText(MainActivity.this, "要播放的视频文件不存在", Toast.LENGTH_SHORT).show();
            return;
        }

//        final ImageButton btn_play = (ImageButton) findViewById(R.id.btn_play); //获取“播放/暂停”按钮
//        final ImageButton btn_stop = (ImageButton) findViewById(R.id.btn_stop); //获取“停止”按钮
//        btn_play.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (video.isPlaying()) {
//                    video.pause();
//                    btn_play.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.play, null));
//
//                } else {
//                    //实现继续播放与暂停播放
//                    video.start();
//                    btn_play.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.pause, null));
//                }
//            }
//        });
//
//        btn_stop.setOnClickListener(new View.OnClickListener() {  //实现停止播放
//            @Override
//            public void onClick(View v) {
//                video.stopPlayback();
//                btn_play.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.play, null));
//                video.setVideoPath(file.getAbsolutePath());         //指定要播放的视频
//            }
//        });

//        为VideoView添加完成事件监听器，实现视频播放结束后的提示信息
        video.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                //弹出消息提示框显示播放完毕
                Toast.makeText(MainActivity.this, "视频播放完毕！", Toast.LENGTH_SHORT).show();
//                btn_play.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.play, null));
            }
        });

        //创建android.widget.MediaController对象，控制视频的播放
        MediaController mc = new MediaController(this);
        //实现视频的播放功能
        video.setMediaController(mc);                       //设置VideoView与MediaController相关联
        video.requestFocus();                               //让VideoView获得焦点
        video.start();

    }
}
